package yto.freemarker.springaop.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Proxy;

/**
 *
 * bean的初始化之前的调用的方法
 *
 * @author dongxing
 **/
//@Component
public class AopProcessor implements BeanPostProcessor {


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //bean是原生的bean
        if (beanName.equals("service")) {
            Class[] classes = {bean.getClass().getInterfaces()[0]};
            YtoInvationHandler ytoInvationHandler = new YtoInvationHandler(bean);
            //代理后的bean
            bean=Proxy.newProxyInstance(this.getClass().getClassLoader(), classes, ytoInvationHandler);

        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }
}
